import com.cbw.bean.Book;
import com.cbw.dao.BookDao;
import com.cbw.service.UserService;
import com.cbw.view.Views;
import java.util.ArrayList;

public class Main {
    static Views v = new Views();
    static UserService userService = new UserService();
    static BookDao bookDao = new BookDao();

    public static void main(String[] args) {
        v.welcome();
        //主流程：1.登录，2退出
        //如果登录成功，进入2级菜单：增删改查，返回
        //查三级菜单：模糊查询，3个排序，1.返回
        boolean flag = true;
        while (flag) {
            v.printMainMenu();
            int key = v.inputChoose(2);
            switch (key) {
                case 1:
                    login();
                    break;
                case 2:
                    flag = false;
                    v.bye();
                    break;
            }
        }
    }

    private static void login() {
        String username = v.inputUsername();
        String password = v.inputPassword();
        boolean flag = userService.login(username, password);
        if (flag) {
            v.loginSuccess();
            adminLoop();

        } else {
            v.loginFail();
        }
    }

    private static void adminLoop() {
        boolean flag = true;
        while (flag) {
            v.printAdminMenu();
            int key = v.inputChoose(5);
            switch (key) {
                case 1:
                    addBook();
                    break;
                case 2:
                    removeBook();
                    break;
                case 3:
                    modifyBook();
                    break;
                case 4:
                    showBooks();
                    break;
                case 5:
                    flag = false;
                    v.backMain();
                    break;
            }
        }
    }

    private static void showBooks() {
        boolean flag = true;
        while (flag) {
            v.printShowMenu();
            int key = v.inputChoose(5);
            switch (key) {
                case 1:
                    queryBook();
                    break;
                case 2:
                    sortBookByPriceDown();
                    break;
                case 3:
                    sortBookByPriceUp();
                    break;
                case 4:
                    sortByPublishDate();
                    break;
                case 5:
                    flag = false;
                    v.backAdmin();
                    break;
            }
        }

    }

    private static void sortByPublishDate() {
        ArrayList<Book> list = bookDao.sortByPublishDate();
        v.showBooks(list);
    }

    private static void sortBookByPriceUp() {
        ArrayList<Book> list = bookDao.sortByPriceUp();
        v.showBooks(list);
    }

    private static void sortBookByPriceDown() {
        ArrayList<Book> list = bookDao.sortByPriceDown();
        v.showBooks(list);
    }

    /**
     * 模糊查询,
     */
    private static void queryBook() {
        String bookName = v.inputBookName();
        ArrayList<Book> list = bookDao.queryBooksByName(bookName);
        v.showBooks(list);
    }

    private static void modifyBook() {
        String bookName = v.inputBookName();
        String newBookName = v.inputNewBookName();
        double newBookPrice = v.inputNewBookPrice();
        String newBookDate = v.inputNewBookDate();
        Book newBook = new Book(newBookName, newBookPrice, newBookDate);
        boolean modify = bookDao.modify(bookName, newBook);
        v.printModifyResult(modify);
    }

    private static void removeBook() {
        String bookName = v.inputBookName();
        boolean remove = bookDao.removeBook(bookName);
        v.printRemoveResult(remove);
    }

    private static void addBook() {
        String bookName = v.inputBookName();
        double bookPrice = v.inputBookPrice();
        String bookPublishDate = v.inputBookDate();
        boolean add = bookDao.addBook(new Book(bookName, bookPrice, bookPublishDate));
        v.printAddResult(add);
    }
}